﻿
namespace YouFood.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using YouFood.Web.Model;


    // Implements application logic using the YouFoodEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class OrderCookerDomainService : LinqToEntitiesDomainService<YouFoodEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Orders' query.
        public IQueryable<Order> GetOrders()
        {
            return this.ObjectContext.Orders;
        }

        public IQueryable<Order> GetOrdersCooker()
        {
            return this.ObjectContext.Orders.Where(x=> x.Status == 0 || x.Status == 1); // retourne Order : "a preparer ou en cours de preparation"
        }

        public IQueryable<Order> GetOrdersWaiter()
        {
            return this.ObjectContext.Orders.Where(x => x.Status == 2 || x.Status == 10); // retourne Order : "a livrer + waite"
        }

        public IQueryable<StatDto> GetStat()
        {
            var categoryCounts =
                from p in ObjectContext.Orders
                group p by p.FoodReference.Value.Name into g
                select new StatDto{ Name = g.Key, Count = g.Count() };

            return categoryCounts; 
        }

        public void InsertOrder(Order order)
        {
            if ((order.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(order, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Orders.AddObject(order);
            }
        }

        public void UpdateOrder(Order currentOrder)
        {
            this.ObjectContext.Orders.AttachAsModified(currentOrder, this.ChangeSet.GetOriginal(currentOrder));
        }

        public void DeleteOrder(Order order)
        {
            if ((order.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(order, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Orders.Attach(order);
                this.ObjectContext.Orders.DeleteObject(order);
            }
        }
    }
}


